/*
 * 文件名：ConsumablesAction.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：wuliquan
 * 修改时间：2013-2-25
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.service.consumables.consumablesInfo.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.wootide.cms.common.Constants;
import com.wootide.cms.common.ErrorCodes;
import com.wootide.cms.common.Page;
import com.wootide.cms.common.QueryCondition;
import com.wootide.cms.common.QueryResults;
import com.wootide.cms.common.Result;
import com.wootide.cms.log.syslog.RunLog;
import com.wootide.cms.service.consumables.consumablesInfo.pojo.ConsumablesInfo;
import com.wootide.cms.service.consumables.consumablesInfo.pojo.ConsumablesReqRecord;
import com.wootide.cms.service.consumables.consumablesInfo.pojo.ConsumablesTypeInfo;
import com.wootide.cms.service.consumables.consumablesInfo.service.ConsumablesInfoService;
import com.wootide.cms.service.consumables.consumablesInfo.service.ConsumablesReqRecordService;
import com.wootide.cms.service.consumables.consumablesInfo.service.ConsumablesTypeInfoService;
import com.wootide.cms.service.consumables.supplier.pojo.SupplierInfo;
import com.wootide.cms.service.consumables.supplier.service.SupplierInfoService;
import com.wootide.cms.sys.common.action.BaseAction;

/**
 * 易耗品控制层实现
 * 〈功能详细描述〉
 * @author wuliquan
 * @version 2013-2-25
 * @see ConsumablesInfoAction
 * @since
 */
public class ConsumablesInfoAction extends BaseAction implements
        ServletRequestAware
{
    /**
     * 意义，目的和功能，以及被用到的地方<br>
     */
    private static final long serialVersionUID = 1L;
    
    /**
     * 日志信息
     */
    private RunLog runLog;
    
    /**
     * 
     */
    private ConsumablesInfoService consumablesInfoService;
    
    /**
     * 易耗品类型信息业务实体类
     */
    private ConsumablesTypeInfoService consumablesTypeInfoService;
    
    /**
     * 易耗品领用记录业务实体类
     */
    private ConsumablesReqRecordService consumablesReqRecordService;
    
    /**
     * 
     */
    private SupplierInfoService supplierInfoService;
    
    /**
     * 易耗品信息
     */
    private ConsumablesInfo consumablesInfo;
    
    /**
     * 
     */
    private List<ConsumablesInfo> consumablesInfos;
    
    /**
     * 
     */
    private List<ConsumablesReqRecord> consumablesReqRecords;
    
    /**
     * 易耗品出入库记录
     */
    private ConsumablesReqRecord consumablesReqRecord;
    
    /**
     * 分页信息
     */
    private Page page;
    
    /**
     * 查询条件
     */
    private QueryCondition queryConditon;
    
    /**
     * HTTP请求对象
     */
    private HttpServletRequest request;
    
    /**
     * 结果集
     */
    private List<ConsumablesInfo> results;
    
    /**
     * 易耗品类型结果集
     */
    private List<ConsumablesTypeInfo> consumablesTypeInfos;
    
    /**
     * 供应商结果集
     */
    private List<SupplierInfo> supplierInfos;
    
    /**
     * 
     */
    private String message;
    
    /**
     * 是否操作成功
     */
    private Integer actResult = Constants.ACTRESULT_DEF;
    
    /**
     * 易耗品ID
     */
    private Integer innerIndex;
    
    /**
     * 易耗品集合
     */
    private Integer[] innerIndexs;
    
    /**
     * 获取系统HttpServletRequest实例
     */
    public void setServletRequest(HttpServletRequest request)
    {
        
        this.request = request;
    }
    
    public String doQuery()
    {
        try
        {
            // 如果从菜单进入，即在没有分页信息的情况下，则默认进入首页
            if (null == page)
            {
                page = new Page();
                page.setCurPage(1);
            }
            
            // 调用而业务层进行分页查询
            Result result = consumablesInfoService.queryForPages(page,
                    queryConditon);
            
            int resultCode = result.getResultCode();
            
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                QueryResults queryResults = (QueryResults)result.getResultPojo();
                // 获取服务代码结果集
                runLog.debug(queryResults);
                
                results = queryResults.getResults();
                // 获取分页信息
                page = queryResults.getPage();
                
                // 跳转到分页页面
                return SUCCESS;
            }
            else
            {
                message = getText(Constants.MESSAGE_PREFIX + resultCode);
                return ERROR;
            }
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * 
     * Description: 预添加<br>
     * Implement: <br>
     * @return
     * @see
     * @return String 跳转标识
     * @author wuliquan
     */
    public String toAdd()
    {
        try
        {
            runLog.debug("consumablesInfo=" + consumablesInfo);
            
            // 调用业务层进行前转MO路由新增操作
            Result consumablesTypeInfoResult = consumablesTypeInfoService.queryAllConsumablesTypeInfos();
            
            Result supplierInfoResult = supplierInfoService.queryAllSuppliers();
            
            int supplierInfoResultCode = supplierInfoResult.getResultCode();
            
            int consumablesTypeInfoResultCode = consumablesTypeInfoResult.getResultCode();
            
            if (ErrorCodes.ERRORCODE_SUCC == consumablesTypeInfoResultCode
                    && ErrorCodes.ERRORCODE_SUCC == supplierInfoResultCode)
            {
                QueryResults supplierInfoResults = (QueryResults)supplierInfoResult.getResultPojo();
                
                QueryResults consumablesTypeInfoResults = (QueryResults)consumablesTypeInfoResult.getResultPojo();
                // 获取服务代码结果集
                runLog.debug(consumablesTypeInfoResults);
                runLog.debug(supplierInfoResults);
                
                consumablesTypeInfos = consumablesTypeInfoResults.getResults();
                
                supplierInfos = supplierInfoResults.getResults();
                
                return SUCCESS;
            }
            else
            {
                message = getText(Constants.MESSAGE_PREFIX
                        + supplierInfoResultCode + ","
                        + Constants.MESSAGE_PREFIX
                        + consumablesTypeInfoResultCode);
                return ERROR;
            }
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            actResult = Constants.ACTRESULT_FAIL;
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * 
     * Description: 添加易耗品信息<br>
     * Implement: <br>
     * @see
     * @return String 跳转标识
     * @author wuliquan
     */
    public String doAdd()
    {
        try
        {
            runLog.debug("consumablesInfo=" + consumablesInfo);
            
            // 调用业务层进行前转MO路由新增操作
            Result result = consumablesInfoService.addOneConsumablesInfo(consumablesInfo,
                    request);
            
            int resultCode = result.getResultCode();
            message = getText(Constants.MESSAGE_PREFIX + resultCode);
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                actResult = Constants.ACTRESULT_SUC;
                return SUCCESS;
            }
            else
            {
                actResult = Constants.ACTRESULT_FAIL;
                return ERROR;
            }
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            actResult = Constants.ACTRESULT_FAIL;
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * 
     * Description: 预入库<br>
     * Implement: <br>
     * @return
     * @see
     * @return String 跳转标识
     * @author wuliquan
     */
    public String toImport()
    {
        try
        {
            runLog.debug("consumablesInfo=" + consumablesInfo);
            
            // 调用业务层进行前转MO路由新增操作
            Result consumablesInfoResult = consumablesInfoService.queryAllConsumablesInfos();
            
            int consumablesInfoResultCode = consumablesInfoResult.getResultCode();
            
            if (ErrorCodes.ERRORCODE_SUCC == consumablesInfoResultCode)
            {
                QueryResults consumablesInfoResults = (QueryResults)consumablesInfoResult.getResultPojo();
                
                
                // 获取服务代码结果集
                runLog.debug(consumablesInfoResults);
                
                consumablesInfos = consumablesInfoResults.getResults();
                
                return SUCCESS;
            }
            else
            {
                message = getText(Constants.MESSAGE_PREFIX
                        + consumablesInfoResultCode);
                return ERROR;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            runLog.error("Unknow Error!", e, request);
            actResult = Constants.ACTRESULT_FAIL;
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * 
     * Description: 添加易耗品入库信息<br>
     * Implement: <br>
     * @see
     * @return String 跳转标识
     * @author wuliquan
     */
    public String doImport()
    {
        try
        {
            runLog.debug("consumablesReqRecord=" + consumablesReqRecord);
            
            // 调用业务层进行前转MO路由新增操作
            Result result = consumablesReqRecordService.addOneConsumablesReqRecord(consumablesReqRecord, request);
            
            int resultCode = result.getResultCode();
            message = getText(Constants.MESSAGE_PREFIX + resultCode);
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                actResult = Constants.ACTRESULT_SUC;
                return SUCCESS;
            }
            else
            {
                actResult = Constants.ACTRESULT_FAIL;
                return ERROR;
            }
        }
        
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            actResult = Constants.ACTRESULT_FAIL;
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * Description: 删除易耗品信息<br>
     * Implement: <br>
     * @return
     * @see
     * @return String
     * @author wuliquan
     */
    public String doDel()
    {
        try
        {
            runLog.debug("doDel");
            // 调用业务层进行删除操作
            Result result = consumablesInfoService.delConsumablesInfo(innerIndexs,
                    request);
            int resultCode = result.getResultCode();
            
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                message = getText(Constants.MESSAGE_PREFIX + resultCode);
                actResult = Constants.ACTRESULT_SUC;
                return SUCCESS;
            }
            // 如果删除中出现失败
            else
            {
                // 取得删除失败的条数
                int delErrorNum = result.getDelErrorNum();
                // 根据总条数和失败条数得出成功的条数
                int delSuccNum = innerIndexs.length - delErrorNum;
                // 定义国际化消息的参数
                String[] params = new String[] {String.valueOf(delSuccNum),
                        String.valueOf(delErrorNum)};
                message = getText(Constants.MESSAGE_PREFIX + resultCode, params);
                actResult = Constants.ACTRESULT_FAIL;
                return ERROR;
            }
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            actResult = Constants.ACTRESULT_FAIL;
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * Description: 预修改易耗品信息<br>
     * Implement: <br>
     * @return
     * @see
     * @return String 跳转标识
     * @author wuliquan
     */
    public String toMod()
    {
        try
        {
            // 调用业务层获取一个路由对象
            Result result = consumablesInfoService.viewOneConsumablesInfo(innerIndex);
            int resultCode = result.getResultCode();
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                consumablesInfo = (ConsumablesInfo)result.getResultPojo();
                return SUCCESS;
            }
            else
            {
                message = getText(Constants.MESSAGE_PREFIX + resultCode);
                return ERROR;
            }
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * Description: 修改易耗品信息<br>
     * Implement: <br>
     * @return
     * @see
     * @return String 跳转标识
     * @author wuliquan
     */
    public String doMod()
    {
        try
        {
            // 调用业务层进行路由的修改操作
            Result result = consumablesInfoService.modOneConsumablesInfo(consumablesInfo,
                    request);
            int resultCode = result.getResultCode();
            message = getText(Constants.MESSAGE_PREFIX + resultCode);
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                actResult = Constants.ACTRESULT_SUC;
                return SUCCESS;
            }
            else
            {
                actResult = Constants.ACTRESULT_FAIL;
                return ERROR;
            }
        }
        
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            actResult = Constants.ACTRESULT_FAIL;
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * Description: 查看易耗品信息详情<br>
     * Implement: <br>
     * @return
     * @see
     * @return String 跳转标识
     * @author wuliquan
     */
    public String toDetail()
    {
        try
        {
            // 调用业务层获取一个路由对象
            Result consumablesReqRecordResult = consumablesReqRecordService.queryConsumablesReqRecordsById(innerIndex);
            
            int consumablesReqRecordresultCode = consumablesReqRecordResult.getResultCode();
            
            if (ErrorCodes.ERRORCODE_SUCC == consumablesReqRecordresultCode)
            {
                QueryResults consumablesReqRecordResults = (QueryResults)consumablesReqRecordResult.getResultPojo();
                
                // 获取服务代码结果集
                runLog.debug(consumablesReqRecordResults);
                
                consumablesReqRecords = consumablesReqRecordResults.getResults();
                
                return SUCCESS;
            }
            else
            {
                message = getText(Constants.MESSAGE_PREFIX
                        + consumablesReqRecordresultCode);
                return ERROR;
            }
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * Description: 预出库易耗品信息<br>
     * Implement: <br>
     * @return
     * @see
     * @return String 跳转标识
     * @author wuliquan
     */
    public String toExport()
    {
        try
        {
            // 调用业务层获取一个路由对象
            Result result = consumablesInfoService.viewOneConsumablesInfo(innerIndex);
            int resultCode = result.getResultCode();
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                consumablesInfo = (ConsumablesInfo)result.getResultPojo();
                return SUCCESS;
            }
            else
            {
                message = getText(Constants.MESSAGE_PREFIX + resultCode);
                return ERROR;
            }
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * Description: 易耗品出库<br>
     * Implement: <br>
     * @return
     * @see
     * @return String 跳转标识
     * @author wuliquan
     */
    public String doExport()
    {
        try
        {
            // 打印方法入口参数
            runLog.debug("consumablesInfo=" + consumablesInfo);
            runLog.debug("consumablesReqRecord=" + consumablesReqRecord);
            
            // 调用业务层进行路由的修改操作
            Result result = consumablesInfoService.exportConsumablesInfo(consumablesReqRecord,
                    innerIndex);
            
            int resultCode = result.getResultCode();
            message = getText(Constants.MESSAGE_PREFIX + resultCode);
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                actResult = Constants.ACTRESULT_SUC;
                return SUCCESS;
            }
            else
            {
                actResult = Constants.ACTRESULT_FAIL;
                return ERROR;
            }
        }
        
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            actResult = Constants.ACTRESULT_FAIL;
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    public String statistic()
    {
        try
        {
            return SUCCESS;
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    public RunLog getRunLog()
    {
        return runLog;
    }
    
    public void setRunLog(RunLog runLog)
    {
        this.runLog = runLog;
    }
    
    public Page getPage()
    {
        return page;
    }
    
    public void setPage(Page page)
    {
        this.page = page;
    }
    
    public QueryCondition getQueryConditon()
    {
        return queryConditon;
    }
    
    public void setQueryConditon(QueryCondition queryConditon)
    {
        this.queryConditon = queryConditon;
    }
    
    public HttpServletRequest getRequest()
    {
        return request;
    }
    
    public void setRequest(HttpServletRequest request)
    {
        this.request = request;
    }
    
    public List<ConsumablesInfo> getResults()
    {
        return results;
    }
    
    public void setResults(List<ConsumablesInfo> results)
    {
        this.results = results;
    }
    
    public String getMessage()
    {
        return message;
    }
    
    public void setMessage(String message)
    {
        this.message = message;
    }
    
    public void setConsumablesInfoService(
            ConsumablesInfoService consumablesInfoService)
    {
        this.consumablesInfoService = consumablesInfoService;
    }
    
    public ConsumablesInfo getConsumablesInfo()
    {
        return consumablesInfo;
    }
    
    public void setConsumablesInfo(ConsumablesInfo consumablesInfo)
    {
        this.consumablesInfo = consumablesInfo;
    }
    
    public Integer getActResult()
    {
        return actResult;
    }
    
    public void setActResult(Integer actResult)
    {
        this.actResult = actResult;
    }
    
    public Integer getInnerIndex()
    {
        return innerIndex;
    }
    
    public void setInnerIndex(Integer innerIndex)
    {
        this.innerIndex = innerIndex;
    }
    
    public Integer[] getInnerIndexs()
    {
        return innerIndexs;
    }
    
    public void setInnerIndexs(Integer[] innerIndexs)
    {
        this.innerIndexs = innerIndexs;
    }
    
    public void setConsumablesTypeInfoService(
            ConsumablesTypeInfoService consumablesTypeInfoService)
    {
        this.consumablesTypeInfoService = consumablesTypeInfoService;
    }
    
    public List<ConsumablesTypeInfo> getConsumablesTypeInfos()
    {
        return consumablesTypeInfos;
    }
    
    public void setConsumablesTypeInfos(
            List<ConsumablesTypeInfo> consumablesTypeInfos)
    {
        this.consumablesTypeInfos = consumablesTypeInfos;
    }
    
    public void setSupplierInfoService(SupplierInfoService supplierInfoService)
    {
        this.supplierInfoService = supplierInfoService;
    }
    
    public List<SupplierInfo> getSupplierInfos()
    {
        return supplierInfos;
    }
    
    public void setSupplierInfos(List<SupplierInfo> supplierInfos)
    {
        this.supplierInfos = supplierInfos;
    }
    
    public ConsumablesReqRecord getConsumablesReqRecord()
    {
        return consumablesReqRecord;
    }
    
    public void setConsumablesReqRecord(
            ConsumablesReqRecord consumablesReqRecord)
    {
        this.consumablesReqRecord = consumablesReqRecord;
    }
    
    public void setConsumablesReqRecordService(
            ConsumablesReqRecordService consumablesReqRecordService)
    {
        this.consumablesReqRecordService = consumablesReqRecordService;
    }

    public List<ConsumablesReqRecord> getConsumablesReqRecords()
    {
        return consumablesReqRecords;
    }

    public void setConsumablesReqRecords(
            List<ConsumablesReqRecord> consumablesReqRecords)
    {
        this.consumablesReqRecords = consumablesReqRecords;
    }

    public List<ConsumablesInfo> getConsumablesInfos()
    {
        return consumablesInfos;
    }

    public void setConsumablesInfos(List<ConsumablesInfo> consumablesInfos)
    {
        this.consumablesInfos = consumablesInfos;
    }
    
}
